﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using Core.Security;
using Model;
using Controller.Enum;
using Newtonsoft.Json.Linq;


namespace Controller.Helper
{
    public static class Member
    {

        //private static DCSalePriceDataContext dataContext = new DCSalePriceDataContext();

        public static string getFullNameByUserName(string username)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                if (username != null)
                {
                    tblUser user = (from alluser in dataContext.tblUsers
                                    where alluser.UserName.Equals(username)
                                    select alluser).First();

                    if (user != null)
                    {
                        return user.FirstName + " " + user.Lastname;
                    }

                }
                return "";
            }
        }


        public static List<tblRole> GetRoleDb()
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var allRole = from allrole in dataContext.tblRoles
                              select allrole;
                List<tblRole> allRoles = allRole.ToList<tblRole>();

                return allRoles;
            }
        }
        public static string GetUserDBjson()
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                JObject k = JObject.FromObject(new
                                                   {
                                                       RMember = from u in dataContext.tblUsers
                                                                 join role in dataContext.tblRoles on u.RoleName equals
                                                                     role.RoleName
                                                                 select new
                                                                            {
                                                                                u.UserName,
                                                                                u.DomainName,
                                                                                u.FirstName,
                                                                                u.Lastname,
                                                                                u.Email,
                                                                                u.IsDisable,
                                                                                u.CreateDate,
                                                                                u.ModifyDate,
                                                                                u.msalegrp,
                                                                                u.Tel,
                                                                                u.Fax,
                                                                                role.RoleName
                                                                            }
                                                   });


                return k.ToString();
            }
        }

        public static List<tblUser> GetUserDB()
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var allUser = from alluser in dataContext.tblUsers
                              select alluser;
                List<tblUser> allMUsers = allUser.ToList<tblUser>();

                return allMUsers;
            }
        }
        public static string[] GetUserNameBySalegroup(string salegrp)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var allUser = from alluser in dataContext.tblUsers
                              where alluser.msalegrp.Equals(salegrp)
                              select alluser;

                List<tblUser> allMUsers = allUser.ToList<tblUser>();
                if (allMUsers.Count > 0)
                {
                    return allUser.Select(u => u.UserName).ToArray();
                }
                else
                {
                    string[] username = { salegrp };
                    return username;

                }
            }

        }

        public static List<tblRole> GetRoleDb(string roleName)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var allRole = from allrole in dataContext.tblRoles
                              where allrole.RoleName == roleName
                              select allrole;
                List<tblRole> allMUsers = allRole.ToList<tblRole>();

                return allMUsers;
            }
        }
        public static List<tblUser> GetUserDB(string username)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var allUser = from alluser in dataContext.tblUsers
                              where alluser.UserName == username
                              select alluser;
                List<tblUser> allMUsers = allUser.ToList<tblUser>();

                return allMUsers;
            }
        }

        public static int RoleAction(List<tblRole> roles, EnumAction action)
        {
            if (roles != null && roles.Count > 0)
            {
                using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
                {
                    switch (action)
                    {
                        case EnumAction.Update:
                            try
                            {
                                foreach (tblRole newrole in roles)
                                {
                                    tblRole oldRole = dataContext.tblRoles.Single(role => role.RoleID == newrole.RoleID);
                                    oldRole.RoleName = newrole.RoleName;
                                    oldRole.RoleDescription = newrole.RoleDescription;
                                    oldRole.IsDisable = newrole.IsDisable;
                                }
                                dataContext.SubmitChanges();
                                return roles.Count;
                            }
                            catch (Exception ex)
                            {

                                return 0;
                            }
                        case EnumAction.Insert:
                            try
                            {
                                foreach (tblRole role in roles)
                                {
                                    dataContext.tblRoles.InsertOnSubmit(role);
                                }
                                dataContext.SubmitChanges();
                                return roles.Count;

                            }
                            catch (Exception ex)
                            {
                                return 0;
                            }
                        case EnumAction.Delete:
                            try
                            {
                                var role1 = from deleterole in dataContext.tblRoles
                                            where roles.Contains(deleterole)
                                            select deleterole;
                                //dataContext.tblUsers.DeleteByPK<tblUser>(users.AsQueryable());
                                int deletedRole = dataContext.tblRoles.DeleteBatch<tblRole>(role1);
                                //dataContext.tblUsers.UpdateBatch<tblUser>(users1, p => new tblUser{ Email = p.DateCreated, 
                                //           UserName = "Chris Cavanagh" } );
                                //dataContext.tblUsers.DeleteAllOnSubmit(users);
                                //dataContext.SubmitChanges();
                                return deletedRole;
                            }
                            catch (Exception)
                            {

                                return 0;
                            }
                    }
                }
            }
            else
            {
                return 0;
            }
            return 0;
        }

        public static int UserAction(List<tblUser> users, EnumAction action)
        {
            if (users != null && users.Count > 0)
            {
                using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
                {
                    switch (action)
                    {
                        case EnumAction.Update:
                            try
                            {
                                foreach (tblUser newuser in users)
                                {
                                    tblUser oldUser =
                                        dataContext.tblUsers.Single(user => user.UserName == newuser.UserName);
                                    //oldUser.UserName = newuser.UserName;
                                    oldUser.RoleName = newuser.RoleName;
                                    oldUser.IsDisable = newuser.IsDisable;
                                    oldUser.msalegrp = newuser.msalegrp.Trim();
                                    oldUser.ModifyDate = DateTime.Now;
                                    oldUser.Tel = newuser.Tel;
                                    oldUser.Fax = newuser.Fax;
                                    if (!string.IsNullOrEmpty(newuser.ImageLicense))
                                    {
                                        oldUser.ImageLicense = newuser.ImageLicense;
                                    }
                                    if (!string.IsNullOrEmpty(newuser.ImageLicenseRelative))
                                    {
                                        oldUser.ImageLicenseRelative = newuser.ImageLicenseRelative;
                                    }
                                }
                                dataContext.SubmitChanges();
                                return users.Count;

                            }
                            catch (Exception ex)
                            {

                                return 0;
                            }
                        case EnumAction.Insert:
                            try
                            {
                                foreach (tblUser user in users)
                                {
                                    dataContext.tblUsers.InsertOnSubmit(user);
                                }
                                dataContext.SubmitChanges();
                                return users.Count;

                            }
                            catch (Exception ex)
                            {

                                return 0;
                            }
                        case EnumAction.Delete:
                            try
                            {
                                var users1 = from deleteuser in dataContext.tblUsers
                                             where users.Contains(deleteuser)
                                             select deleteuser;
                                //int kkk = dataContext.tblUsers.DeleteByPK<tblUser>(users);
                                int result = dataContext.tblUsers.DeleteBatch<tblUser>(users1);
                                //dataContext.tblUsers.UpdateBatch<tblUser>(users1, p => new tblUser{ Email = p.DateCreated, 
                                //           UserName = "Chris Cavanagh" } );
                                //dataContext.tblUsers.DeleteAllOnSubmit(users);
                                //dataContext.SubmitChanges();
                                return result;
                            }
                            catch (Exception)
                            {

                                return 0;
                            }
                    }
                }
            }
            else
            {
                return 0;
            }
            return 0;
        }

        public static int VerifyPincode(string pincode)
        {
            string username = Controller.AD.LdapAuthentication.GetCurrentUser();
            System.Text.Encoding enc = System.Text.Encoding.ASCII;
            string saltvalue = WebConfigurationManager.AppSettings["SaltValue"] ?? "Reddotz";
            byte[] salt = enc.GetBytes(saltvalue);
            var hashpinCode = Core.Security.Security.ComputeHash(pincode,
                                                                 HashAlgorithmEnum.MD5,
                                                                 salt);
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                var allPin = (from pin in nawaDataContext.tblUserPinCodes
                              where pin.UserName.Equals(username) && pin.Pincode.Equals(hashpinCode)
                              select pin).Count();

                if (allPin == 0)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
        }

        public static int ReNewPincode(string pincode)
        {
            string username = Controller.AD.LdapAuthentication.GetCurrentUser();
            System.Text.Encoding enc = System.Text.Encoding.ASCII;
            string saltvalue = WebConfigurationManager.AppSettings["SaltValue"] ?? "Reddotz";
            byte[] salt = enc.GetBytes(saltvalue);
            var hashpinCode = Core.Security.Security.ComputeHash(pincode,
                                                                 HashAlgorithmEnum.MD5,
                                                                 salt);
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {

                var allPin = (from pin in nawaDataContext.tblUserPinCodes
                              where pin.UserName.Equals(username)
                              select pin).Count();
                var newPincode = new tblUserPinCode
                                     {
                                         Pincode = hashpinCode,
                                         UserName = username,
                                         ModifyDate = DateTime.Now,
                                         ModifyUser = username
                                     };

                if (allPin == 0)
                {
                    nawaDataContext.tblUserPinCodes.InsertOnSubmit(newPincode);
                    nawaDataContext.SubmitChanges();
                    return 1;
                }
                else
                {
                    tblUserPinCode oldPincode = nawaDataContext.tblUserPinCodes.Single(user => user.UserName == newPincode.UserName);
                    //oldPincode.UserName = newPincode.UserName;
                    oldPincode.Pincode = newPincode.Pincode;
                    oldPincode.ModifyDate = newPincode.ModifyDate;
                    oldPincode.ModifyUser = newPincode.ModifyUser;
                    nawaDataContext.SubmitChanges();
                    return 1;
                }

                return -1;
            }
        }
    }
}
